package org.javaboy.httpfirewall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;

import java.util.Arrays;

@Configuration
public class SecurityConfig {

    @Bean
    HttpFirewall httpFirewall() {
        StrictHttpFirewall firewall = new StrictHttpFirewall();
//        firewall.setAllowedHttpMethods(Arrays.asList("GET", "POST"));
        //允许所有的请求方法
        firewall.setUnsafeAllowAnyHttpMethod(true);
        //是否允许分号
        firewall.setAllowSemicolon(true);
        firewall.setAllowedHostnames(h -> h.equals("local.javaboy.org"));
        return firewall;
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(a -> a.requestMatchers("/hello").permitAll().anyRequest().authenticated())
                .formLogin(Customizer.withDefaults())
                .csrf(c -> c.disable());
        return http.build();
    }
}
